package com.ahao.servlet.clazz;

import com.ahao.model.RespBean;
import com.ahao.model.RespPageBean;
import com.ahao.service.ClazzService;
import com.ahao.utils.CommonsUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/clazz")
public class ClazzServlet extends HttpServlet {
    ClazzService clazzService = new ClazzService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if ("page".equals(action)) {
            req.getRequestDispatcher("/WEB-INF/jsp/clazz/clazzList.jsp").forward(req, resp);
        } else if ("data".equals(action)) {
            //获取年级的id
            String gid = req.getParameter("gid");
            //分页页码
            String page = req.getParameter("page");
            //每页查询多少条
            String rows = req.getParameter("rows");
            String sort = req.getParameter("sort");
            String order = req.getParameter("order");
            RespPageBean respPageBean = clazzService.getClazzByPage(gid, page, rows, sort, order);
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.write(new ObjectMapper().writeValueAsString(respPageBean));
        }else if ("data_from_teacher".equals(action)) {
            //获取年级的id
            String gid = req.getParameter("gid");
            RespPageBean respPageBean = clazzService.getClazzByPage(gid, null,null, null,null);
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.write(new ObjectMapper().writeValueAsString(respPageBean.getRows()));
        }
    }


    /**
     * 添加班级
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String clazzName = req.getParameter("clazzName");
        String gid = req.getParameter("gid");
        Integer i = clazzService.addClazz(clazzName, gid);

        resp.setContentType("application/json;charset=UTF-8");
        RespBean respBean = null;
        if (i == CommonsUtils.UPDATE_SUCCESS) {
            respBean = RespBean.ok("添加班级成功!");
        }else if (i == CommonsUtils.ILLEGAL_PARAM){
            respBean = RespBean.error("非法请求！");
        }else if(i == CommonsUtils.TABLE_DATA_REPEAT){
            respBean = RespBean.error("班级名称重复!");
        }else if (i == CommonsUtils.OTHER_EXCEPTION) {
            respBean = RespBean.error("添加班级失败!");
        }else {
            respBean = RespBean.error("添加失败!");
        }

        resp.getWriter().write(new ObjectMapper().writeValueAsString(respBean));
    }

    /**
     * 根据班级id删除班级
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        Integer i = clazzService.deleteClazz(cid);
        resp.setContentType("application/json;charset=UTF-8");
        RespBean respBean = null;
        if (i == CommonsUtils.UPDATE_SUCCESS) {
            respBean = RespBean.ok("删除班级成功!");
        }else if (i == CommonsUtils.ILLEGAL_PARAM){
            respBean = RespBean.error("非法请求！");
        }else if (i == CommonsUtils.OTHER_EXCEPTION) {
            respBean = RespBean.error("删除班级失败!");
        }else {
            respBean = RespBean.error("删除失败!");
        }
        resp.getWriter().write(new ObjectMapper().writeValueAsString(respBean));
    }
}
